package com.daykw.vip.api.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daykw.vip.api.model.Page;
import com.daykw.vip.api.model.Result;
import com.daykw.vip.api.service.VipApiService;
import com.daykw.vip.api.util.Const;

@Controller
@RequestMapping("/vip/admster/xhs")
public class VipApiAction {
	private static Logger logger = Logger.getLogger(VipApiAction.class);
	@Resource
	private VipApiService vipApiService;

	@ResponseBody
	@RequestMapping(value = "/getArticleByUid")
	public Page getArticleByUid(HttpServletRequest request, HttpServletResponse response, String user_id,
			Integer pageNum, Integer pageSize, String token) throws Exception {
		Page page = new Page();
		try {
			if (StringUtils.isEmpty(user_id)) {
				page.setResult(Const.FAIL, "aid 参数错误，没有赋值");
				return page;
			}

			if (pageSize == null) {
				pageSize = 1000;
			}
			if (pageNum == null || pageNum == 0) {
				pageNum = 1;
			}

			if (StringUtils.isNotEmpty(token) && token.equals(Const.ADMASTER_TOKEN)) {
				page = vipApiService.getArticleByUid(user_id, pageNum, pageSize);
			} else {
				page.setResult(Const.FAIL, "token 秘钥错误");
			}

		} catch (Exception e) {
			page.setCode(Const.FAIL_400);
			logger.error("", e);
		}
		return page;
	}

	@ResponseBody
	@RequestMapping(value = "/getRestList")
	public Result getRestList(HttpServletRequest request, HttpServletResponse response, String aids, String urls,
			String token, String astatus, String cstatus) throws Exception {
		Result ret = new Result();
		try {
			if (StringUtils.isEmpty(aids) && StringUtils.isEmpty(urls)) {
				ret.setResult(Const.FAIL, "aid和url 参数错误，没有赋值");
				return ret;
			}
			if (StringUtils.isNotEmpty(aids) && StringUtils.isNotEmpty(urls)) {
				ret.setResult(Const.FAIL, "aid 和url 参数错误，不能同时赋值");
				return ret;
			}
			String[] as = {};
			String[] us = {};
			if (StringUtils.isNotEmpty(token) && token.equals(Const.ADMASTER_TOKEN)) {
				if (aids != null) {
					as = aids.split(",");
				} else {
					us = urls.split(",");
				}
				ret = vipApiService.getRestList(as, us, astatus, astatus);
			} else {
				ret.setResult(Const.FAIL, "token 秘钥错误");
			}

		} catch (Exception e) {
			ret.setCode(Const.FAIL_400);
			logger.error("", e);
		}
		return ret;
	}

	@ResponseBody
	@RequestMapping(value = "/getArticleByAid")
	public Result getArticleByAid(HttpServletRequest request, HttpServletResponse response, String aid, String token)
			throws Exception {
		Result ret = new Result();
		try {
			if (StringUtils.isEmpty(aid)) {
				ret.setResult(Const.FAIL, "aid 参数错误，没有赋值");
				return ret;
			}

			if (StringUtils.isNotEmpty(token) && token.equals(Const.ADMASTER_TOKEN)) {
				ret = vipApiService.getArticleByAid(aid);
			} else {
				ret.setResult(Const.FAIL, "token 秘钥错误");
			}

		} catch (Exception e) {
			ret.setCode(Const.FAIL_400);
			logger.error("", e);
		}
		return ret;
	}

	@ResponseBody
	@RequestMapping(value = "/restAid")
	public Result restUrl(HttpServletRequest request, HttpServletResponse response, String aids, 
			String token) throws Exception {
		Result ret = new Result();
		try {
			if (StringUtils.isEmpty(aids)) {
				ret.setResult(Const.FAIL, "aids 参数错误，没有赋值");
				return ret;
			}

			if (StringUtils.isNotEmpty(token) && token.equals(Const.ADMASTER_TOKEN)) {

				String[] us = aids.split(",");
				if (us.length > 100) {
					ret.setResult(Const.FAIL, "aids一次重置不能超过100个");
					return ret;
				}

				

				// if (vipApiService.getRestMaxDate()) {
				ret = vipApiService.restAids(us);
				// } else {
				// ret.setResult(Const.FAIL, "接口调用太频繁了，请您10分钟后在试试:)");
				// }

			} else {
				ret.setResult(Const.FAIL, "token 秘钥错误");
			}

		} catch (Exception e) {
			ret.setCode(Const.FAIL_400);
			logger.error("", e);
		}
		return ret;
	}

	@ResponseBody
	@RequestMapping(value = "/getArticleByAids")
	public Result getArticleByAids(HttpServletRequest request, HttpServletResponse response, String aids, String token)
			throws Exception {
		Result ret = new Result();
		try {
			if (StringUtils.isEmpty(aids)) {
				ret.setResult(Const.FAIL, "aids 参数错误，没有赋值");
				return ret;
			}

			String[] aid = aids.split(",");
			if (aid.length > 1000) {
				ret.setResult(Const.FAIL, "aids 参数过多，最多1000个");
				return ret;
			}
			if (aid.length <= 1) {
				ret.setResult(Const.FAIL, "aids 参数过少，最少2个");
				return ret;
			}

			if (StringUtils.isNotEmpty(token) && token.equals(Const.ADMASTER_TOKEN)) {
				ret = vipApiService.getArticleByAids(aid);
			} else {
				ret.setResult(Const.FAIL, "token 秘钥错误");
			}

		} catch (Exception e) {
			ret.setCode(Const.FAIL_400);
			logger.error("", e);
		}
		return ret;
	}

	@ResponseBody
	@RequestMapping(value = "/getCommentsByAid")
	public Result getCommentsByAid(HttpServletRequest request, HttpServletResponse response, String aid, String token)
			throws Exception {
		Result ret = new Result();
		try {
			if (StringUtils.isEmpty(aid)) {
				ret.setResult(Const.FAIL, "aid 参数错误，没有赋值");
				return ret;
			}

			if (StringUtils.isNotEmpty(token) && token.equals(Const.ADMASTER_TOKEN)) {
				ret = vipApiService.getCommentsByAid(aid);
			} else {
				ret.setResult(Const.FAIL, "token 秘钥错误");
			}

		} catch (Exception e) {
			ret.setCode(Const.FAIL_400);
			logger.error("", e);
		}
		return ret;
	}

	/**
	 * 根据创建时间获取文章列表
	 * 
	 * @param request
	 * @param response
	 * @param aid
	 * @param token
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/getArticleByCreateDate")
	public Page getArticleByCreateDate(HttpServletRequest request, HttpServletResponse response, String cdate,
			Integer pageNum, Integer pageSize, String token) throws Exception {
		Page ret = new Page();
		try {
			if (StringUtils.isEmpty(cdate)) {
				ret.setResult(Const.FAIL, "cdate参数错误，没有赋值");
				return ret;
			}
			if (pageNum == null || pageNum < 1) {
				pageNum = 1;
			}
			if (pageSize == null || pageSize < 2 || pageSize > 1000) {
				ret.setResult(Const.FAIL, "ps,page_size参数错误，应该在[2~1000]的范围");
				return ret;
			}
			DateFormat format = new SimpleDateFormat("yyyyMMddHH");
			try {
				format.parse(cdate);
			} catch (Exception e) {
				ret.setResult(Const.FAIL, "cdate日期格式错误");
				return ret;
			}
			if (StringUtils.isNotEmpty(token) && token.equals(Const.ADMASTER_TOKEN)) {
				ret = vipApiService.getArticleByCreateDate(Integer.valueOf(cdate), pageNum, pageSize);
			} else {
				ret.setResult(Const.FAIL, "token 秘钥错误");
			}

		} catch (Exception e) {
			ret.setCode(Const.FAIL_400);
			logger.error("", e);
		}
		return ret;
	}

	/**
	 * 根据文章发布日期获取文章列表
	 * 
	 * @param request
	 * @param response
	 * @param aid
	 * @param token
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/getArticleByPubDate")
	public Page getArticleByPubDate(HttpServletRequest request, HttpServletResponse response, String pdate,
			Integer pageNum, Integer pageSize, String token) throws Exception {
		Page ret = new Page();
		try {
			if (StringUtils.isEmpty(pdate)) {
				ret.setResult(Const.FAIL, "cdate参数错误，没有赋值");
				return ret;
			}
			if (pageNum == null || pageNum < 1) {
				pageNum = 1;
			}
			if (pageSize == null || pageSize < 2 || pageSize > 1000) {
				ret.setResult(Const.FAIL, "ps,page_size参数错误，应该在[2~1000]的范围");
				return ret;
			}

			DateFormat format = new SimpleDateFormat("yyyyMMdd");
			try {
				format.parse(pdate);
			} catch (Exception e) {
				ret.setResult(Const.FAIL, "pdate日期格式错误");
				return ret;
			}
			if (StringUtils.isNotEmpty(token) && token.equals(Const.ADMASTER_TOKEN)) {
				ret = vipApiService.getArticleByPuhDate(Integer.valueOf(pdate), pageNum, pageSize);
			} else {
				ret.setResult(Const.FAIL, "token 秘钥错误");
			}

		} catch (Exception e) {
			ret.setCode(Const.FAIL_400);
			logger.error("", e);
		}
		return ret;
	}

	/**
	 * 根据文章发布日期获取文章列表
	 * 
	 * @param request
	 * @param response
	 * @param aid
	 * @param token
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/getAuthorbyUserid")
	public Result getAuthorbyUserid(HttpServletRequest request, HttpServletResponse response, String userid,
			String token) throws Exception {
		Result ret = new Result();
		try {
			if (StringUtils.isEmpty(userid)) {
				ret.setResult(Const.FAIL, "userid参数错误，没有赋值");
				return ret;
			}

			if (StringUtils.isNotEmpty(token) && token.equals(Const.ADMASTER_TOKEN)) {
				ret = vipApiService.getAuthorbyUserid(userid);
			} else {
				ret.setResult(Const.FAIL, "token 秘钥错误");
			}

		} catch (Exception e) {
			ret.setCode(Const.FAIL_400);
			logger.error("", e);
		}
		return ret;
	}

	/**
	 * 根据文章发布日期获取文章列表
	 * 
	 * @param request
	 * @param response
	 * @param aid
	 * @param token
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/getAuthorbyUserids")
	public Result getAuthorbyUserids(HttpServletRequest request, HttpServletResponse response, String userids,
			String token) throws Exception {
		Result ret = new Result();
		try {
			if (StringUtils.isEmpty(userids)) {
				ret.setResult(Const.FAIL, "userid参数错误，没有赋值");
				return ret;
			}
			String[] uids = userids.split(",");
			if (uids == null || uids.length == 0) {
				ret.setResult(Const.FAIL, "userid参数错误，没有赋值");
				return ret;
			}
			if (StringUtils.isNotEmpty(token) && token.equals(Const.ADMASTER_TOKEN)) {
				ret = vipApiService.getAuthorbyUserids(uids);
			} else {
				ret.setResult(Const.FAIL, "token 秘钥错误");
			}

		} catch (Exception e) {
			ret.setCode(Const.FAIL_400);
			logger.error("", e);
		}
		return ret;
	}

	/**
	 * 根据文章发布日期获取文章列表
	 * 
	 * @param request
	 * @param response
	 * @param aid
	 * @param token
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/adduids")
	public Result addUids(HttpServletRequest request, HttpServletResponse response, String uids, String token)
			throws Exception {
		Result ret = new Result();
		try {
			if (StringUtils.isEmpty(uids)) {
				ret.setResult(Const.FAIL, "uids参数错误，没有赋值");
				return ret;
			}
			String[] uid = uids.split(",");
			if (uid == null || uid.length == 0 || uid.length > 100) {
				ret.setResult(Const.FAIL, "uid参数错误，没有赋值,或者大于100个");
				return ret;
			}
			if (StringUtils.isNotEmpty(token) && token.equals(Const.ADMASTER_TOKEN)) {
				ret = vipApiService.addUids(uid);
			} else {
				ret.setResult(Const.FAIL, "token 秘钥错误");
			}

		} catch (Exception e) {
			ret.setCode(Const.FAIL_400);
			logger.error("", e);
		}
		return ret;
	}

}
